package jgl.vajra.operator.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import jgl.vajra.operator.widget.retrofithelper.rxsubscriber.CommonObserver;

public class PushDataService extends Service {

    private String latitudeLongitude;

    public static void startService(Context context, String latitudeLongitude) {
        Intent intent = new Intent(context, PushDataService.class);
        intent.putExtra("latitudeLongitude",latitudeLongitude);
        context.startService(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Logger.d( "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        latitudeLongitude = intent.getStringExtra("latitudeLongitude");
        init();
        return super.onStartCommand(intent, flags, startId);
    }

    public void init() {

        startPushLatitudeLongitude();
    }

    private void startPushLatitudeLongitude() {
        Observable.interval(5, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new CommonObserver() {
            @Override
            public void onNext(Object o) {
                super.onNext(o);
                Logger.d(  "推送经纬度"+latitudeLongitude);
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.d(  "onDestroy");
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
